﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireResponseBLL
    {
        #region 构造单例
        private FireResponseBLL() { }
        private static FireResponseBLL _instance;
        public static FireResponseBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireResponseBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Add(Fireresponse info)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
                return result;
            }
            var lastRecordInfo = BLL.RecordBLL.Instance.GetLast(fireInfo.FireId, null);
            if (lastRecordInfo == null
                || (lastRecordInfo.FirstOrDefault().Type == (int)RecordType.火灾首报))
            {
                result.Status = -2;
                result.Message = "FireInfo Status Is Not Added";
                return result;
            }
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Fireresponse.Where(p => p.FireId == info.FireId && p.Level == info.Level).FirstOrDefault();
                if (exist != null)
                {
                    result.Status = -2;
                    result.Message = "Fireresponse Level Is Exist";
                    return result;
                }
                ef.Fireresponse.Add(info);
                ef.Entry<Fireresponse>(info).State = EntityState.Added;
                ef.SaveChanges();

                var userInfo = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();

                //处置记录
                Record record = new Record();
                record.FireId = fireInfo.FireId;
                record.HandleTime = info.TransDatetime;
                record.Type = (int)RecordType.启动响应;
                record.UserId = fireInfo.UserId;
                var leveName = SundryHelper.GetLoumaNumber(info.Level);
                string mrContent = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value),fireInfo.FireName, leveName);
                record.Content = mrContent;
                record.AddTime = info.TransDatetime;
                record.Ip = fireInfo.Ip;
                record.Applicant = userInfo.RealName;
                
                record.CustomData = info.ResponseId.ToString();
                ef.Record.Add(record);
                ef.Entry<Record>(record).State = EntityState.Added;

                //消息
                Message message = new Message();
                message.FireId = fireInfo.FireId;
                message.Type = (int)MessageType.启动响应;
                message.Content = mrContent;
                message.SendType = (int)MessageSendType.上级向下级发送;
                message.IsRead = 0;
                message.HandleTime = info.TransDatetime;
                message.SendUserId = info.UserId;
                message.TargetUserId = 0;
                message.Ip = fireInfo.Ip;
                ef.Message.Add(message);
                ef.Entry<Message>(message).State = EntityState.Added;
                ef.SaveChanges();


                #region 装载消息
                List<Message> messages = new List<Message>() { message };
                result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                #endregion
                return result;
            }
           
        }


        public List<Fireresponse> Get(int fireId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Fireresponse.Where(p => p.FireId == fireId).OrderBy(o => o.Level).ToList();
            }
        }
    }
}
